package org.example.iol;

import java.io.*;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class D {
    public static void sortText(String target,String newPath) throws IOException {
        //定义缓冲流输入对象
        BufferedReader br=new BufferedReader(new FileReader(target));
        String s = null;
        //定义TreeMap，会自动排序
        TreeMap<String,String> treeMap = new TreeMap();
        while ((s = br.readLine())!=null){ //判断是否为空
//            System.out.println("读取到的文本："+s);
            //拆分文本
            String[] split = s.split("\\.");//根据参数字符分割字符，返回字符串数组
            //将拆分的文本存入map集合中
            treeMap.put(split[0],split[1]);
        }
        //遍历集合
        Set<Map.Entry<String,String>> entrySet = treeMap.entrySet();
        //定义缓冲输出流对象
        BufferedWriter bw = new BufferedWriter(new FileWriter(newPath));
        for (Map.Entry<String, String> entry : entrySet) {
            //写出数据
            bw.write(entry.getKey()+"、"+entry.getValue());
            bw.write("\n");
        }
        bw.flush(); //刷新
        bw.close(); //关闭
//        System.out.println("++++++++++++++++++++");
//        System.out.println("排序后的：" + treeMap);

        br.close();
    }

    public static void main(String[] args) throws IOException {
        sortText("E:/IDEA-file/出师表.txt","E:/IDEA-file/出师表整理版.txt");
    }
}